﻿package controllers 
{
	import com.adobe.utils.DateUtil;
	import events.AlbumEvent;
	import events.QueryErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import model.Album;
	import model.MainModel;
	import model.Photo;
	import utils.VkQuery;
	
	/**
	 * ...
	 * @author Slavshik Alexander
	 */
	public class AlbumController extends BaseController {
		
		[Event(name="ainited", type="events.AlbumEvent")]
		
		private var loaded:int = -1;
		public static const ALBUMS_LOADED:String = "aloaeded";
		public static const ALL_ALBUMS_INITED:String = "allinited";
		
		public function AlbumController() {
		}
		public function getAlbums():void {
			if (MainModel.user.haveNoPhotos) {
				dispatchEvent(new QueryErrorEvent(QueryErrorEvent.NO_PHOTOS));
				return;
			}
			MainModel.albums = [];
			stopQueries();
			if (MainModel.user.albums.length > 0) {
				MainModel.albums = MainModel.user.albums;
				//dispatchEvent();
				loaded = -1;
				urlLoader.addEventListener(Event.COMPLETE, onLoaded);
				loadNext();
				return;
			}
			var urlRequest:URLRequest = new URLRequest(VkQuery.getPhotoAlbums());
			urlLoader.addEventListener(Event.COMPLETE, onComplete);
			VkQuery.loadStackQuery(urlLoader, urlRequest);     
		}
		override public function stopQueries():void {
			super.stopQueries();
			urlLoader.removeEventListener(Event.COMPLETE, onComplete);
			urlLoader.removeEventListener(Event.COMPLETE, onLoaded);
		}
		private function buildAlbums(albums:XMLList):void {
			MainModel.albums = [];
			for each (var album:* in albums) {
				var a:Album = new Album();
				a.id = album.aid;
				a.thumb = int(album.thumb_id);
				a.owner = int(album.owner_id);
				a.title = String(album.title);
				a.description = String(album.description);
				a.created = int(album.created);
				a.updated = int(album.updated);
				a.size = int(album.size);
				a.privacy = int(album.privacy);
				MainModel.albums.push(a);
				MainModel.user.albums.push(a);
			}
			dispatchEvent(new Event(ALBUMS_LOADED));
			urlLoader.removeEventListener(Event.COMPLETE, onComplete);
			urlLoader.addEventListener(Event.COMPLETE, onLoaded);
			loaded = -1;
			loadNext();
		}
		private function loadNext():void {
			loaded++;
			var album:Album = MainModel.albums[loaded];
			if (album == null) {
				return;
			}
			if (album.loaded) {
				loadNext();
				return;
			}
			var urlRequest:URLRequest = new URLRequest(VkQuery.getPhoto(album.owner, album.id));
			VkQuery.loadStackQuery(urlLoader, urlRequest)
		}
		private function onLoaded(e:Event):void {
			if (!checkXML(e.currentTarget.data)) return;
			var xmlData:XML = new XML(e.currentTarget.data);
			var xmlList:XMLList = xmlData.children();
			var album:Album = MainModel.albums[loaded];
			album.photos = [];
			album.loaded = true;
			for (var i:int = 0; i < xmlList.length(); i++) {
				var p:Photo = new Photo();
				p.aid = int(xmlList[i].aid);
				p.pid = int(xmlList[i].pid);
				p.src = String(xmlList[i].src);
				p.src_big = String(xmlList[i].src_big);
				p.src_small = String(xmlList[i].src_small);
				p.created = int(xmlList[i].created);
				if (p.pid == album.thumb) album.thumbPhoto = p;
				album.photos.push(p);
			}
			dispatchEvent(new AlbumEvent(AlbumEvent.ALBUM_INITED, album));
			if (loaded >= MainModel.albums.length - 1) {
				dispatchEvent(new Event(ALL_ALBUMS_INITED));
				urlLoader.removeEventListener(Event.COMPLETE, onLoaded);
				return;
			}
			loadNext();
		}
		private function onComplete(e:Event):void {
			if (!checkXML(e.target.data)) return;
			var xmlData:XML = new XML(e.target.data);
			if (String(xmlData) == "") {
				MainModel.user.haveNoPhotos = true;
				dispatchEvent(new QueryErrorEvent(QueryErrorEvent.NO_PHOTOS));
				return;
			}
			buildAlbums(xmlData.album);
		}
	}
	
}